package com.example.xuegengwang.xuegengwang.wxapi;

import android.content.Context;

import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import common.Constant;
import common.MyApplication;
import pay.bean.WxPayBean;
import utils.ToastUtils;
import view.WaitingDialog;

/**
 * Created by Administrator on 2017/4/19.
 */

public class MyWxApi {
    public static IWXAPI iwxapi;//微信登录的接口
    public static IWXAPI iwxPayapi;//微信登录的接口
    public static void registerApp(Context context){
        iwxapi = WXAPIFactory.createWXAPI(context, Constant.WEIXIN_APP_ID, false);
        iwxapi.registerApp(Constant.WEIXIN_APP_ID);
    }

    public static boolean Login(){
        boolean sIsWXAppInstalledAndSupported = iwxapi.isWXAppInstalled();
        if(sIsWXAppInstalledAndSupported){
            SendAuth.Req req = new SendAuth.Req();
            req.scope = "snsapi_userinfo";
            req.state = "jinzhao";
            iwxapi.sendReq(req);
            return true;
        }else{
            ToastUtils.shortToast("未安装微信");
            return false;
        }
    }

    public static void Pay(Context context,WxPayBean wxPayBean){
        iwxPayapi = WXAPIFactory.createWXAPI(context, Constant.WEIXIN_APP_ID);
        iwxPayapi.registerApp(Constant.WEIXIN_APP_ID);

        boolean sIsWXAppInstalledAndSupported = iwxPayapi.isWXAppInstalled();

        if(sIsWXAppInstalledAndSupported) {
            PayReq request = new PayReq();
            request.appId = wxPayBean.getAppId();
            request.partnerId = wxPayBean.getPartnerid();
            request.prepayId = wxPayBean.getPrepayid();
            request.packageValue = "Sign=WXPay";
            request.nonceStr = wxPayBean.getNonceStr();
            request.timeStamp = wxPayBean.getTimeStamp();
            request.sign = wxPayBean.getSign();
            iwxPayapi.sendReq(request);
        }else{
            ToastUtils.shortToast("未安装微信");
        }
    }

}
